package com.tz.farm_feeding.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.annotation.JsonAlias;
import com.tz.farm_feeding.domain.Batch;
import com.tz.farm_feeding.domain.BatchExample;
import com.tz.farm_feeding.domain.Pig;
import com.tz.farm_feeding.domain.PigExample;
import com.tz.farm_feeding.domain.status;
import com.tz.farm_feeding.mapper.BatchMapper;
import com.tz.farm_feeding.mapper.PigMapper;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

@RestController
@RequestMapping("/pig")
public class PigController {

  @Autowired
  PigMapper pigMapper;

  @Autowired
  BatchMapper batchmapper;

  @PostMapping(value = "/getAllPig")
  public String getAllPig(@RequestBody JSONObject jsonparm, HttpServletRequest request, HttpSession session) {
    JSONObject resp = new JSONObject();
    List<Pig> pigs = pigMapper.selectByExample(new PigExample());
    JSONArray jsonArray = new JSONArray();
    for (Pig pig : pigs) {
      JSONObject jsonObject = new JSONObject();
      jsonObject.put("basedata", JSONObject.parse(pig.toString()));
      Batch batch = batchmapper.selectByPrimaryKey(pig.getBatchid());
      jsonObject.put("batchdata", JSONObject.parse(batch.toString()));
      jsonArray.add(jsonObject);

    }
    resp.put("result", status.s_ok);
    resp.put("data", jsonArray);

    return resp.toJSONString();
  }

  @PostMapping(value = "/addPig")
  public String addPig(@RequestBody JSONObject jsonparm, HttpServletRequest request, HttpSession session) {
    JSONObject resp = new JSONObject();

    Pig pig = new Pig();
    pig.setAge(jsonparm.getInteger("age"));
    pig.setWeight(jsonparm.getDouble("weight"));
    pig.setStatus(jsonparm.getString("status"));
    pig.setIdsn(jsonparm.getString("idsn"));
    pig.setInfo(jsonparm.getString("info"));
    pig.setBatchid(jsonparm.getInteger("batchid"));
    pig.setIsipv(jsonparm.getString("isipv"));

    pigMapper.insert(pig);
    resp.put("result", status.s_ok);
    return resp.toJSONString();
  }

  @PostMapping(value = "/delPig")
  public String delPig(@RequestBody JSONObject jsonparm, HttpServletRequest request, HttpSession session) {
    JSONObject resp = new JSONObject();
    int id = jsonparm.getInteger("id");
    pigMapper.deleteByPrimaryKey(id);
    resp.put("result", status.s_ok);
    return resp.toJSONString();
  }

  
  @PostMapping(value = "/updatePig")
  public String updatePig(@RequestBody JSONObject jsonparm, HttpServletRequest request, HttpSession session) {
    JSONObject resp = new JSONObject();
    int id = jsonparm.getInteger("id");
    Pig pig = new Pig();
    pig.setId(id);
    pig.setAge(jsonparm.getInteger("age"));
    pig.setWeight(jsonparm.getDouble("weight"));
    pig.setStatus(jsonparm.getString("status"));
    pig.setIdsn(jsonparm.getString("idsn"));
    pig.setInfo(jsonparm.getString("info"));
    pig.setBatchid(jsonparm.getInteger("batchid"));
    pig.setIsipv(jsonparm.getString("isipv"));
 
    pigMapper.updateByPrimaryKeySelective(pig);
    resp.put("result", status.s_ok);
    return resp.toJSONString();
  }

  @PostMapping(value = "/deadPig")
  public String deadPig(@RequestBody JSONObject jsonparm, HttpServletRequest request, HttpSession session) {
    JSONObject resp = new JSONObject();
    int id = jsonparm.getInteger("id");
    PigExample pigExample = new PigExample();
    PigExample.Criteria criteria = pigExample.createCriteria();
    criteria.andIdEqualTo(id);
    Pig pig = new Pig();
    pig.setStatus("死亡");
    pigMapper.updateByExampleSelective(pig, pigExample);
    resp.put("result", status.s_ok);
    return resp.toJSONString();
  }

  @PostMapping(value = "/sellPig")
  public String sellPig(@RequestBody JSONObject jsonparm, HttpServletRequest request, HttpSession session) {
    JSONObject resp = new JSONObject();
    int id = jsonparm.getInteger("id");
    PigExample pigExample = new PigExample();
    PigExample.Criteria criteria = pigExample.createCriteria();
    criteria.andIdEqualTo(id);
    criteria.andStatusEqualTo("待售");
    Pig record = new Pig();
    record.setStatus("售出");
    pigMapper.updateByExampleSelective(record, pigExample);
    resp.put("result", status.s_ok);
    return resp.toJSONString();
  }

  @PostMapping(value = "/ipvPig")
  public String ipvPig(@RequestBody JSONObject jsonparm, HttpServletRequest request, HttpSession session) {
    JSONObject resp = new JSONObject();
    int id = jsonparm.getInteger("id");
    String isipv = jsonparm.getString("isipv");

    PigExample pigExample = new PigExample();
    PigExample.Criteria criteria = pigExample.createCriteria();
    criteria.andIdEqualTo(id);
    Pig record = new Pig();
    record.setIsipv(isipv );
    pigMapper.updateByExampleSelective(record, pigExample);
    resp.put("result", status.s_ok);
    return resp.toJSONString();
  }
}
